home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / demos / walker / walkviewer.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  13KB  |  491 lines

  1. /*
  2.  * walkviewer.c [from agviewer.c  (version 1.0)]
  3.  *
  4.  * AGV: a glut viewer. Routines for viewing a 3d scene w/ glut
  5.  *
  6.  * See agv_example.c and agviewer.h comments within for more info.
  7.  *
  8.  * I welcome any feedback or improved versions!
  9.  *
  10.  * Philip Winston - 4/11/95
  11.  * pwinston@hmc.edu
  12.  * http://www.cs.hmc.edu/people/pwinston
  13.  */
  14.  
  15. #include <GL/glut.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <math.h>
  19.  
  20. #include "walkviewer.h"
  21.  
  22. /***************************************************************/
  23. /************************** SETTINGS ***************************/
  24. /***************************************************************/
  25.  
  26.    /* Initial polar movement settings */
  27. #define INIT_POLAR_AZ  0.0
  28. #define INIT_POLAR_EL 30.0
  29. #define INIT_DIST      3.0
  30. #define INIT_AZ_SPIN   0.5
  31. #define INIT_EL_SPIN   0.0
  32.  
  33.   /* Initial flying movement settings */
  34. #define INIT_EX        0.0
  35. #define INIT_EY       -2.0
  36. #define INIT_EZ       -2.0
  37. #define INIT_MOVE     0.01
  38. #define MINMOVE      0.001    
  39.  
  40.   /* Start in this mode */
  41. #define INIT_MODE   POLAR   
  42.  
  43.   /* Controls:  */
  44.  
  45.   /* map 0-9 to an EyeMove value when number key is hit in FLYING mode */
  46. #define SPEEDFUNCTION(x) ((x)*(x)*0.001)  
  47.  
  48.   /* Multiply EyeMove by (1+-MOVEFRACTION) when +/- hit in FLYING mode */
  49. #define MOVEFRACTION 0.25   
  50.  
  51.   /* What to multiply number of pixels mouse moved by to get rotation amount */
  52. #define EL_SENS   0.5
  53. #define AZ_SENS   0.5
  54.  
  55.   /* What to multiply number of pixels mouse moved by for movement amounts */
  56. #define DIST_SENS 0.01
  57. #define E_SENS    0.01
  58.  
  59.   /* Minimum spin to allow in polar (lower forced to zero) */
  60. #define MIN_AZSPIN 0.1
  61. #define MIN_ELSPIN 0.1
  62.  
  63.   /* Factors used in computing dAz and dEl (which determine AzSpin, ElSpin) */
  64. #define SLOW_DAZ 0.90
  65. #define SLOW_DEL 0.90
  66. #define PREV_DAZ 0.80
  67. #define PREV_DEL 0.80
  68. #define CUR_DAZ  0.20
  69. #define CUR_DEL  0.20
  70.  
  71. /***************************************************************/
  72. /************************** GLOBALS ****************************/
  73. /***************************************************************/
  74.  
  75. int     MoveMode = INIT_MODE;  /* FLYING or POLAR mode? */
  76.  
  77. GLfloat Ex = INIT_EX,             /* flying parameters */
  78.         Ey = INIT_EY,
  79.         Ez = INIT_EZ,
  80.         EyeMove = INIT_MOVE,     
  81.  
  82.         EyeDist = INIT_DIST,      /* polar params */
  83.         AzSpin  = INIT_AZ_SPIN,
  84.         ElSpin  = INIT_EL_SPIN,
  85.  
  86.         EyeAz = INIT_POLAR_AZ,    /* used by both */
  87.         EyeEl = INIT_POLAR_EL;
  88.  
  89. int agvMoving;    /* Currently moving?  */
  90.  
  91. int downx, downy,   /* for tracking mouse position */
  92.     lastx, lasty,
  93.     downb = -1;     /* and button status */
  94.                         
  95. GLfloat downDist, downEl, downAz, /* for saving state of things */
  96.         downEx, downEy, downEz,   /* when button is pressed */
  97.         downEyeMove;                
  98.  
  99. GLfloat dAz, dEl, lastAz, lastEl;  /* to calculate spinning w/ polar motion */
  100. int     AdjustingAzEl = 0;
  101.  
  102. int AllowIdle, RedisplayWindow; 
  103.    /* If AllowIdle is 1 it means AGV will install its own idle which
  104.     * will update the viewpoint as needed and send glutPostRedisplay() to the
  105.     * window RedisplayWindow which was set in agvInit().  AllowIdle of 0
  106.     * means AGV won't install an idle funciton, and something like
  107.     * "if (agvMoving) agvMove()" should exist at the end of the running
  108.     * idle function.
  109.     */
  110.  
  111. #define PI            3.14159265358979323846
  112. #define MAX(x,y) (((x) > (y)) ? (x) : (y))
  113. #define TORAD(x) ((PI/180.0)*(x))
  114. #define TODEG(x) ((180.0/PI)*(x))
  115.  
  116. /***************************************************************/
  117. /************************ PROTOTYPES ***************************/
  118. /***************************************************************/
  119.  
  120.   /*
  121.    * these are functions meant for internal use only
  122.    * the other prototypes are in agviewer.h
  123.    */
  124.  
  125. void PolarLookFrom(GLfloat dist, GLfloat elevation, GLfloat azimuth);
  126. void FlyLookFrom(GLfloat x, GLfloat y, GLfloat z, GLfloat az, GLfloat el);
  127. int  ConstrainEl(void);
  128. void MoveOn(int v);
  129. void SetMove(float newmove);
  130. void normalize(GLfloat v[3]);
  131. void ncrossprod(float v1[3], float v2[3], float cp[3]);
  132.  
  133.  
  134. /***************************************************************/
  135. /************************ agvInit ******************************/
  136. /***************************************************************/
  137.  
  138. void agvInit(int window)
  139. {
  140.   glutMouseFunc(agvHandleButton);
  141.   glutMotionFunc(agvHandleMotion);
  142.   glutKeyboardFunc(agvHandleKeys);
  143.   RedisplayWindow = glutGetWindow();
  144.   agvSetAllowIdle(window);
  145. }
  146.  
  147. /***************************************************************/
  148. /************************ VIEWPOINT STUFF **********************/
  149. /***************************************************************/
  150.  
  151.   /*
  152.    * viewing transformation modified from page 90 of red book
  153.    */
  154. void PolarLookFrom(GLfloat dist, GLfloat elevation, GLfloat azimuth)
  155. {
  156.   glTranslatef(0, 0, -dist);
  157.   glRotatef(elevation, 1, 0, 0);
  158.   glRotatef(azimuth, 0, 1, 0);
  159.  
  160. }
  161.  
  162.   /*
  163.    * I took the idea of tracking eye position in absolute
  164.    * coords and direction looking in Polar form from denis
  165.    */
  166. void FlyLookFrom(GLfloat x, GLfloat y, GLfloat z, GLfloat az, GLfloat el)
  167. {
  168.   float lookat[3], perp[3], up[3];
  169.  
  170.   lookat[0] = sin(TORAD(az))*cos(TORAD(el));
  171.   lookat[1] = sin(TORAD(el));
  172.   lookat[2] = -cos(TORAD(az))*cos(TORAD(el));
  173.   normalize(lookat);
  174.   perp[0] = lookat[2];
  175.   perp[1] = 0;
  176.   perp[2] = -lookat[0];
  177.   normalize(perp);
  178.   ncrossprod(lookat, perp, up);
  179.   gluLookAt(x, y, z,
  180.             x+lookat[0], y+lookat[1], z+lookat[2],
  181.             up[0], up[1], up[2]);
  182. }
  183.  
  184.   /*
  185.    * Call viewing transformation based on movement mode
  186.    */
  187. void agvViewTransform(void)
  188.   switch (MoveMode) {
  189.     case FLYING:
  190.       FlyLookFrom(Ex, Ey, Ez, EyeAz, EyeEl);
  191.       break;
  192.     case POLAR:
  193.       PolarLookFrom(EyeDist, EyeEl, EyeAz);
  194.       break;
  195.     }
  196. }
  197.  
  198.   /*
  199.    * keep them vertical; I think this makes a lot of things easier, 
  200.    * but maybe it wouldn't be too hard to adapt things to let you go
  201.    * upside down
  202.    */
  203. int ConstrainEl(void)
  204. {
  205.   if (EyeEl <= -90) {
  206.     EyeEl = -89.99;
  207.     return 1;
  208.   } else if (EyeEl >= 90) {
  209.     EyeEl = 89.99;
  210.     return 1;
  211.   }
  212.   return 0;
  213. }
  214.  
  215.  /*
  216.   * Idle Function - moves eyeposition
  217.   */
  218. void agvMove(void)
  219. {
  220.  
  221.   switch (MoveMode)  {
  222.     case FLYING:
  223.       Ex += EyeMove*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
  224.       Ey += EyeMove*sin(TORAD(EyeEl));
  225.       Ez -= EyeMove*cos(TORAD(EyeAz))*cos(TORAD(EyeEl));
  226.       break;
  227.  
  228.     case POLAR:
  229.       EyeEl += ElSpin;
  230.       EyeAz += AzSpin;
  231.       if (ConstrainEl()) {  /* weird spin thing to make things look     */
  232.         ElSpin = -ElSpin;      /* look better when you are kept from going */
  233.                                /* upside down while spinning - Isn't great */
  234.         if (fabs(ElSpin) > fabs(AzSpin))
  235.           AzSpin = fabs(ElSpin) * ((AzSpin > 0) ? 1 : -1);
  236.       }
  237.       break;
  238.     }
  239.  
  240.   if (AdjustingAzEl) {
  241.     dAz *= SLOW_DAZ;
  242.     dEl *= SLOW_DEL;
  243.   }
  244.  
  245.   if (AllowIdle) {
  246.     glutSetWindow(RedisplayWindow);
  247.     glutPostRedisplay();
  248.   }
  249. }
  250.  
  251.  
  252.   /*
  253.    * Don't install agvMove as idle unless we will be updating the view
  254.    * and we've been given a RedisplayWindow
  255.    */
  256. void MoveOn(int v)
  257. {
  258.   if (v && ((MoveMode == FLYING && EyeMove != 0) ||
  259.              (MoveMode == POLAR &&
  260.              (AzSpin != 0 || ElSpin != 0 || AdjustingAzEl)))) {
  261.     agvMoving = 1;
  262.     if (AllowIdle)
  263.       glutIdleFunc(agvMove);
  264.   } else {
  265.     agvMoving = 0;
  266.     if (AllowIdle)
  267.       glutIdleFunc(NULL);
  268.   }
  269. }
  270.  
  271.   /*
  272.    * set new redisplay window.  If <= 0 it means we are not to install
  273.    * an idle function and will rely on whoever does install one to 
  274.    * put statement like "if (agvMoving) agvMove();" at end of it
  275.    */
  276. void agvSetAllowIdle(int allowidle)
  277. {
  278.   if ((AllowIdle = allowidle))
  279.     MoveOn(1);
  280. }
  281.  
  282.  
  283.   /*
  284.    * when moving to flying we stay in the same spot, moving to polar we
  285.    * reset since we have to be looking at the origin (though a pivot from
  286.    * current position to look at origin might be cooler)
  287.    */
  288. void agvSwitchMoveMode(int move)
  289. {
  290.   switch (move) {
  291.     case FLYING:
  292.       Ex    = -EyeDist*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
  293.       Ey    =  EyeDist*sin(TORAD(EyeEl));
  294.       Ez    =  EyeDist*(cos(TORAD(EyeAz))*cos(TORAD(EyeEl)));
  295.       EyeAz =  EyeAz;
  296.       EyeEl = -EyeEl;
  297.       EyeMove = INIT_MOVE;
  298.       break;
  299.     case POLAR:
  300.       EyeDist = INIT_DIST;
  301.       EyeAz   = INIT_POLAR_AZ;
  302.       EyeEl   = INIT_POLAR_EL;
  303.       AzSpin  = INIT_AZ_SPIN;
  304.       ElSpin  = INIT_EL_SPIN;
  305.       break;
  306.     }
  307.   MoveMode = move;
  308.   MoveOn(1);
  309.   glutPostRedisplay();
  310. }
  311.  
  312. /***************************************************************/
  313. /*******************    MOUSE HANDLING   ***********************/
  314. /***************************************************************/
  315.  
  316. void agvHandleButton(int button, int state, int x, int y)
  317. {
  318.  if (state == GLUT_DOWN && downb == -1) {  
  319.     lastx = downx = x;
  320.     lasty = downy = y;
  321.     downb = button;    
  322.  
  323.     switch (button) {
  324.       case GLUT_LEFT_BUTTON:
  325.         lastEl = downEl = EyeEl;
  326.         lastAz = downAz = EyeAz;
  327.         AzSpin = ElSpin = dAz = dEl = 0;
  328.         AdjustingAzEl = 1;
  329.     MoveOn(1);
  330.         break;
  331.  
  332.       case GLUT_MIDDLE_BUTTON:
  333.         downDist = EyeDist;
  334.     downEx = Ex;
  335.     downEy = Ey;
  336.     downEz = Ez;
  337.     downEyeMove = EyeMove;
  338.     EyeMove = 0;
  339.     }
  340.  
  341.   } else if (state == GLUT_UP && button == downb) {
  342.  
  343.     downb = -1;
  344.  
  345.     switch (button) {
  346.       case GLUT_LEFT_BUTTON:
  347.         if (MoveMode != FLYING) {
  348.       AzSpin =  -dAz;
  349.       if (AzSpin < MIN_AZSPIN && AzSpin > -MIN_AZSPIN)
  350.         AzSpin = 0;    
  351.       ElSpin = -dEl;
  352.       if (ElSpin < MIN_ELSPIN && ElSpin > -MIN_ELSPIN)
  353.         ElSpin = 0; 
  354.     }
  355.         AdjustingAzEl = 0;
  356.         MoveOn(1);
  357.     break;
  358.  
  359.       case GLUT_MIDDLE_BUTTON:
  360.     EyeMove = downEyeMove;
  361.       }
  362.   }
  363. }
  364.  
  365.  /*
  366.   * change EyeEl and EyeAz and position when mouse is moved w/ button down
  367.   */
  368. void agvHandleMotion(int x, int y)
  369. {
  370.   int deltax = x - downx, deltay = y - downy;
  371.  
  372.   switch (downb) {
  373.     case GLUT_LEFT_BUTTON:
  374.       EyeEl  = downEl + EL_SENS * ((MoveMode == FLYING) ? -deltay : deltay);
  375.       ConstrainEl();
  376.       EyeAz  = downAz + AZ_SENS * deltax;
  377.       dAz    = PREV_DAZ*dAz + CUR_DAZ*(lastAz - EyeAz);
  378.       dEl    = PREV_DEL*dEl + CUR_DEL*(lastEl - EyeEl);
  379.       lastAz = EyeAz;
  380.       lastEl = EyeEl;
  381.       break;
  382.     case GLUT_MIDDLE_BUTTON:
  383.         EyeDist = downDist + DIST_SENS*deltay;
  384.         Ex = downEx - E_SENS*deltay*sin(TORAD(EyeAz))*cos(TORAD(EyeEl));
  385.         Ey = downEy - E_SENS*deltay*sin(TORAD(EyeEl));
  386.         Ez = downEz + E_SENS*deltay*cos(TORAD(EyeAz))*cos(TORAD(EyeEl));
  387.       break;
  388.   }
  389.   glutPostRedisplay();
  390. }
  391.  
  392. /***************************************************************/
  393. /********************* KEYBOARD HANDLING ***********************/
  394. /***************************************************************/
  395.  
  396.   /*
  397.    * set EyeMove (current speed) for FLYING mode
  398.    */
  399. void SetMove(float newmove)
  400. {
  401.   if (newmove > MINMOVE) {
  402.     EyeMove = newmove;
  403.     MoveOn(1);
  404.   } else {
  405.     EyeMove = 0;
  406.     MoveOn(0);
  407.   }
  408. }
  409.  
  410.   /*
  411.    * 0->9 set speed, +/- adjust current speed  -- in FLYING mode
  412.    */
  413. void agvHandleKeys(unsigned char key, int x, int y)
  414. {
  415.   if (MoveMode != FLYING)
  416.     return;
  417.  
  418.   if (key >= '0' && key <= '9')
  419.     SetMove(SPEEDFUNCTION((key-'0')));
  420.   else
  421.     switch(key) {
  422.       case '+':  
  423.         if (EyeMove == 0)
  424.           SetMove(MINMOVE);
  425.          else
  426.       SetMove(EyeMove *= (1 + MOVEFRACTION));
  427.         break;
  428.       case '-':
  429.     SetMove(EyeMove *= (1 - MOVEFRACTION));
  430.         break;
  431.     }
  432. }
  433.  
  434. /***************************************************************/
  435. /*********************** VECTOR STUFF **************************/
  436. /***************************************************************/
  437.  
  438.   /* normalizes v */
  439. void normalize(GLfloat v[3])
  440. {
  441.   GLfloat d = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  442.  
  443.   if (d == 0)
  444.     fprintf(stderr, "Zero length vector in normalize\n");
  445.   else
  446.     v[0] /= d; v[1] /= d; v[2] /= d;
  447. }
  448.  
  449.   /* calculates a normalized crossproduct to v1, v2 */
  450. void ncrossprod(float v1[3], float v2[3], float cp[3])
  451. {
  452.   cp[0] = v1[1]*v2[2] - v1[2]*v2[1];
  453.   cp[1] = v1[2]*v2[0] - v1[0]*v2[2];
  454.   cp[2] = v1[0]*v2[1] - v1[1]*v2[0];
  455.   normalize(cp);
  456. }
  457.  
  458. /***************************************************************/
  459. /**************************** AXES *****************************/
  460. /***************************************************************/
  461.  
  462.  
  463.   /* draw axes -- was helpful to debug/design things */
  464. void agvMakeAxesList(int displaylistnum)
  465. {
  466.   int i,j;
  467.   GLfloat axes_ambuse[] =   { 0.5, 0.0, 0.0, 1.0 };
  468.   glNewList(displaylistnum, GL_COMPILE);
  469.   glPushAttrib(GL_LIGHTING_BIT);
  470.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, axes_ambuse);
  471.     glBegin(GL_LINES);
  472.       glVertex3f(15, 0, 0); glVertex3f(-15, 0, 0);
  473.       glVertex3f(0, 15, 0); glVertex3f(0, -15, 0);
  474.       glVertex3f(0, 0, 15); glVertex3f(0, 0, -15);
  475.     glEnd();
  476.     for (i = 0; i < 3; i++) {
  477.       glPushMatrix();
  478.         glTranslatef(-10*(i==0), -10*(i==1), -10*(i==2));
  479.         for (j = 0; j < 21; j++) {
  480.           glutSolidCube(0.1);
  481.           glTranslatef(i==0, i==1, i==2);
  482.     }
  483.       glPopMatrix();
  484.     }
  485.   glPopAttrib();
  486.   glEndList();  
  487. }
  488.  
  489.  
  490.